package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.model.data.InspectionDetailModel;
import com.fin.zw.aiqas.model.request.ReconsiderRequest;
import com.fin.zw.aiqas.model.request.ReconsiderationApprovalRequest;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.ReconsiderService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@RestController
@RequestMapping(value = "/reconsider")
@Api(value = "申请复议", description = "申请复议")
public class ReconsiderController {

    private final static Logger log = LoggerFactory.getLogger(ReconsiderController.class);

    @Autowired
    ReconsiderService reconsiderService;//申请复议

    /**
     * 查询申请复议列表(底层为质检结果详情列表)
     */
    @GetMapping(value = "/queryPageInvite")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数（0..N）"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(asc|desc). "),
    })
    @ApiOperation(value = "查询申请复议列表", notes = "查询申请复议列表")
    public ResponseEntity<Page<InspectionDetailModel>> queryReconsiderationApply(ReconsiderationApprovalRequest request, Pageable pageable,@ApiIgnore @CurrentUser SessionModel session) {
        try {
            request.setOperationUserCode(session.getUserCode());
            Integer count = reconsiderService.queryCountReconsiderationApproval(request);
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
            request.setServiceCode(session.getUserCode());
            List<InspectionDetailModel> result = reconsiderService.queryReconsiderationApproval(request);
            Page<InspectionDetailModel> pageResult = new PageImpl<>(result, pageable,count);
            return ResponseEntity.ok().body(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return ResponseEntity.badRequest().build();
        }
    }


    /**
     * 查询复议审批列表(底层为质检结果详情列表)
     */
    @GetMapping(value = "/queryPageApply")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数（0..N）"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(asc|desc). "),
    })
    @ApiOperation(value = "查询申请复议列表", notes = "查询申请复议列表")
    public ResponseEntity<Page<InspectionDetailModel>> queryReconsiderationApproval(ReconsiderationApprovalRequest request, Pageable pageable,@ApiIgnore @CurrentUser SessionModel session) {
        try {
            request.setOperationUserCode(session.getUserCode());
            request.setOperationUserId(session.getUserId());
            Integer count = reconsiderService.queryCountReconsiderationApproval(request);
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
            List<InspectionDetailModel> result = reconsiderService.queryReconsiderationApproval(request);
            Page<InspectionDetailModel> pageResult = new PageImpl<>(result, pageable, count);
            return ResponseEntity.ok().body(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return ResponseEntity.badRequest().build();
        }
    }


    /**
     * 发起复议申请
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "发起复议申请", notes = "发起复议申请")
    public ResponseEntity<Void> addReconsiderationDetails(@RequestBody ReconsiderRequest request, @ApiIgnore @CurrentUser SessionModel session) {
        reconsiderService.addReconsideration(request, session);
        return ResponseEntity.ok().build();
    }


}
